<template>
    <div v-if="Object.keys(param).length">
        <div v-for="item in param.data.components" :key="item.id">
            <zw-image
                v-if="item.options.name === 'images'"
                :options="item.options"
                @jump="jump($event, 1)"
                :isUniapp="true">
            </zw-image>
            <zw-product
                v-if="item.options.name === 'product'"
                :options="item.options"
                :isUniapp="true"
                @jump="jump($event, 2)"
                @addCart="addCart"></zw-product>
            <zw-category
                v-if="item.options.name === 'category'"
                :options="item.options"
                :isUniapp="true"
                @jump="jump($event, 3)"></zw-category>
            <zw-seckill
                v-if="item.options.name === 'seckill'"
                :options="item.options"
                :isUniapp="true"
                @addCart="jump($event, 6)"
                @jump="jump($event, 5)"></zw-seckill>
            <zw-groupbuy
                v-if="item.options.name === 'groupbuy'"
                :options="item.options"
                :isUniapp="true"
                @addCart="jump($event, 6)"
                @jump="jump($event, 5)"></zw-groupbuy>
            <zw-text
                v-if="item.options.name === 'text'"
                :options="item.options"
                :isUniapp="true"
                @jump="jump($event, 1)"></zw-text>
            <zw-wecom
                v-if="item.options.name === 'wecom'"
                :options="item.options"
                :isUniapp="true"
                @jump="jump($event, 7)"></zw-wecom>
            <zw-search
                v-if="item.options.name === 'search'"
                :options="item.options"
                :isUniapp="true"
                @jump="jump($event, 4)"></zw-search>
            <zw-coupon
                v-if="item.options.name === 'coupon'"
                :options="item.options"
                :isUniapp="true"
                @jump="jump($event, 8)"></zw-coupon>
        </div>
        <uni-popup ref="popup" :safe-area="false" @maskClick="change">
            <view class="popup-content" :class="['popup-content', props.hasTabbar ? 'popup_padding_b' : '']">
                <view class="pop_cancel" @click.stop="close">
                    <img class="pop_logo" :src="getImgOssUrl('/ui-prod-2.0.0/cancel_1.1.png')"
                /></view>
                <skupop
                    :goodsId="goodsData.goodsId"
                    :goodsNo="goodsData.goodsNo"
                    :show="goodsData.show"
                    :activityId="goodsData.activityId"
                    :activityStatus="goodsData.activityStatus"
                    @popup="popup.open('bottom')"
                    @close="close"
                    @buyGoods="toConfirm"
            /></view>
        </uni-popup>
    </div>
    <zw-warn :style="{ opacity: isshow ? 1 : 0 }" v-else></zw-warn>
</template>
<script setup>
import { reactive, ref, watch } from 'vue';
import router from '@/router';
import request from '@/request';
import { getImgOssUrl } from '@/commons/utils';
import { useTrace } from '@/commons/trace';
import storage from '@/commons/storage';
import skupop from '@/components/skupop';
import { onHide } from '@dcloudio/uni-app';
const { traceReport } = useTrace({ autoLoad: true });
const emit = defineEmits(['sendStatus', 'numadd', 'openPop', 'closePop', 'updataCoupon']);
const goodsData = reactive({ goodsId: '', goodsNo: '', show: false, activityId: '', activityStatus: '' });
let isshow = ref(false);
let popup = ref(false);
const props = defineProps({
    datas: {},
    pageType: {
        type: String,
        default: 'home'
    },
    hasTabbar: {
        type: Boolean,
        default: false
    }
});
let param = reactive({});
watch(
    () => props.datas,
    val => {
        param = val;

        isshow.value = false;
    },
    { immediate: true, deep: true }
);
onHide(() => {
    if (goodsData.show) {
        popup.value.close();
    }
    goodsData.show = false;
});
const toPath = async item => {
    if (item.mutual == 4 || item.mutual == 5 || item.mutual == 13) {
        uni.switchTab({
            url: item.url
        });
        // 把分类分组id传到本地缓存
        if (item.mutual == 4) {
            storage.set('CATEGORY_ID', item.id || '历史数据');
        }
    } else if (item.mutual == 14) {
        uni.navigateToMiniProgram({
            appId: item.appId,
            path: item.url
        });
    } else if (item.mutual == 18) {
        let shopId = storage.get('ZW_SHOP_ID');
        let { data } = await request('integralConfig', { shopId });
        const url =
            data?.pointGoodsMail?.status === 1
                ? '/pages-user/myIntegral/shoppingMall'
                : '/pages-user/myIntegral/detail';
        router.push(url);
    } else if (item.mutual == 19) {
        uni.previewImage({
            urls: [item.openimg],
            longPressActions: {
                itemList: ['发送给朋友', '保存图片', '收藏']
            }
        });
    } else {
        if (item.url.startsWith('plugin-private://')) {
            uni.navigateTo({
                url: item.url
            });
        } else {
            item.url && router.push(item.url);
        }
    }
};
const jump = (item, index) => {
    if (index == 1) {
        toPath(item);
    } else if (index == 2) {
        router.push('/pages-cart/good/index?goodsNo=' + item.goodsNo + '&goodsId=' + item.id);
    } else if (index == 3) {
        toPath(item);
    } else if (index == 5) {
        let activityId = item.activityId ? item.activityId : item.id;
        router.push(
            '/pages-cart/good/index?source=2&goodsNo=' +
                item.goodsNo +
                '&goodsId=' +
                item.goodsId +
                '&activityId=' +
                activityId
        );
    } else if (index == 6) {
        if (item.onSale == 3) {
            uni.showToast({
                title: '商品已售罄',
                icon: 'none'
            });
            return;
        }
        let activityId = item.data.activityId ? item.data.activityId : item.data.id;
        router.push(
            '/pages-cart/good/index?source=2&goodsNo=' +
                item.data.goodsNo +
                '&goodsId=' +
                item.data.goodsId +
                '&activityId=' +
                activityId
        );
    } else if (index == 8) {
        // 领取优惠券（领取后刷新页面）
        request('acquireCoupon', { couponId: item.couponId })
            .then(res => {
                emit('updataCoupon');
                uni.showToast({
                    title: '领取成功！',
                    icon: 'none'
                });
            })
            .catch(err => {
                emit('updataCoupon');
                uni.showToast({
                    title: err.data.msg,
                    icon: 'none'
                });
            });
    } else if (index == 7) {
        uni.previewImage({
            urls: [item.qwCode],
            longPressActions: {
                itemList: ['发送给朋友', '保存图片', '收藏']
            }
        });
    } else {
        router.push(
            '/pages-home/search/index?contentColor=' +
                item.color +
                '&hotKeyword=' +
                item.title +
                '&position=' +
                item.position
        );
    }
};
const addCart = data => {
    // if (data.data?.skuInfo?.length == 1) {
    //     // sku规格值为1直接购物车 + 1;
    //     traceReport('add_cart', {
    //         productId: data.data.id,
    //         productName: data.data.goodsName,
    //         skuId: data.data.skuInfo[0]?.id,
    //         skuName: '',
    //         num: 1
    //     });
    //     request('cartAddServ', { skuId: data.data.skuInfo[0].id, goodsId: data.data.id, quantity: 1 }).then(res => {
    //         uni.showToast({
    //             title: '添加成功！',
    //             icon: 'none'
    //         });
    //     });
    // } else {
    //     //拉起规则悬浮窗;
    //     goodsData.goodsNo = data.data.goodsNo;
    //     goodsData.goodsId = data.data.id;
    //     goodsData.show = true;
    //     emit('openPop');
    // }
    if (data.data.goodsNo.onSale == 3) {
        uni.showToast({
            title: '商品已售罄',
            icon: 'none'
        });
        return;
    }
    goodsData.goodsNo = data.data.goodsNo;
    goodsData.goodsId = data.data.id;
    goodsData.show = true;
    emit('openPop');
};
const toConfirm = val => {
    let { quantity, skuId, goodsId } = val;
    router.push({
        path: '/pages-cart/confirm/index',
        query: {
            skuList: JSON.stringify([{ goodsId, quantity, skuId }])
        }
    });
};
const change = () => {
    close();
};
const close = () => {
    goodsData.show = false;
    popup.value.close();
    emit('closePop');
};
</script>
<style lang="scss" scoped></style>
